<template>
  <div>
    <h1>购买商品</h1>
    <div>
      <el-descriptions
        class="margin-top"
        title="商品信息"
        :column="3"
        :size="size"
        border
      >
        <template #extra> </template>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">商品名称</div>
          </template>
          {{ prod.prodName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">商品类型</div>
          </template>
          {{ prod.typeName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">品牌</div>
          </template>
          {{ prod.brandName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">单位</div>
          </template>
          <el-tag size="small">{{ prod.unitName }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">供应商</div>
          </template>
          {{ prod.apprName }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template #label>
            <div class="cell-item">图片</div>
          </template>
          <el-image
            :src="prod.prodImage"
            style="width: 50px; height: 50px"
          ></el-image>
        </el-descriptions-item>
      </el-descriptions>
    </div>
    <div>
      <el-button type="primary" @click="onsubmitBuy">马上购买</el-button>
    </div>
    <div>
      <span>您的订单金额为：{{ buyAmount }}元</span>
    </div>
    <div>
      <el-table
        :data="tableData"
        stripe
        @selection-change="handleSelectionChange"
        style="width: 100%"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="infoId" label="ID" />
        <el-table-column prop="prodSku" label="SKU" />
        <el-table-column prop="prodName" label="商品名称" />
        <el-table-column prop="prodPrice" label="售价" />
        <el-table-column prop="prodStock" label="库存" />
        <el-table-column prop="alertStock" label="预警库存" />
        <el-table-column prop="buyNum" label="购买数量" width="200px">
          <template #default="scope">
            <el-input-number
              v-model="scope.row.buyNum"
              :min="0"
              :max="scope.row.prodStock"
              @change="handleChange"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" />
      </el-table>
    </div>
    <div style="margin-top: 20px">
      <el-pagination
        v-model:current-page="searForm.pageNum"
        v-model:page-size="searForm.pageSize"
        :page-sizes="[2, 4, 6, 8]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, computed } from "vue";
import { getProdInfoList, addOrder } from "../api/api";
import { ElMessage, type ComponentSize } from "element-plus";
const size = ref<ComponentSize>("default");
const multipleSelection = ref<any[]>([]);
const prod = ref({});
const tableData = ref([]);
const total = ref(0);
const searForm = ref({
  pageNum: 1,
  pageSize: 2,
  typeId: null,
});

const buyAmount = ref(0);
//多选框回调
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val;
  console.log("handleSelectionChange", multipleSelection.value);
  //计算订单金额
  buyAmount.value = 0;
  multipleSelection.value.forEach((item) => {
    buyAmount.value += item.prodPrice * item.buyNum;
  });
};

//购买
const onsubmitBuy = () => {
  console.log("onsubmitBuy");
  if (multipleSelection.value.length == 0) {
    ElMessage.error("请选择商品");
    return;
  }
  addOrder(multipleSelection.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("购买成功");
      LoadprodList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const handleChange = () => {
  console.log("购买数量");
  buyAmount.value = 0;
  multipleSelection.value.forEach((item) => {
    buyAmount.value += item.prodPrice * item.buyNum;
  });
};
const LoadprodList = () => {
  getProdInfoList(searForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

//分页回调函数
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searForm.value.pageSize = val;
  LoadprodList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searForm.value.pageNum = val;
  LoadprodList();
};

onMounted(() => {
  let s = sessionStorage.getItem("buy");
  prod.value = JSON.parse(s);
  searForm.value.typeId = prod.value.prodId;
  console.log("prodInfo", prod.value);
  LoadprodList();
});
</script>

<style scoped></style>
